#ifndef SLT_SILENT_H
#define SLT_SILENT_H
#include <fcntl.h>
#include <sys/stat.h>
#include <mqueue.h>
#include <sys/mman.h>

#define SLT_TCP_MODE    1
void ***slt_ctx;

#define MAP_MEMSIZE (1024*1024*2)

#define SLT_MAX_HANDLER_FUNC	10
#define SLT_NGINX_EVENT_ACCEPT	1

typedef struct _task_node {
	unsigned long delaytime;
	time_t accept_time;
	zval *cb;
	unsigned char one;
} task_node_t;

typedef struct _event_emit_node {
    int fd;
    int type;
    int from_id;
} event_emit_node_t;

typedef struct _event_node {
	int fd;
	int from_id;
	int len;
	char data[8192];
} event_node;

typedef struct _reactor reactor_t;
typedef void (*handler_func_t)(reactor_t *reactor, event_emit_node_t *emit_node);
typedef int (*add_func_t)(reactor_t *reactor, int fd, int type);
typedef void (*wait_func_t)(reactor_t *reactor);
typedef void (*set_func_t)(reactor_t *reactor, int type, handler_func_t func);
typedef void (*free_func_t)(reactor_t *reactor);
typedef void (*del_func_t)(reactor_t *reactor, int fd);

struct _reactor {
	void *object;
	void *ptr;
	int id;
	handler_func_t handlers[SLT_MAX_HANDLER_FUNC];
	set_func_t set;
	add_func_t add;
	wait_func_t wait;
	free_func_t free;
	del_func_t del;
};

typedef struct _factory factory_t;
typedef int (*slt_dispatch_func_t)(factory_t *factory, int fd, int schedid, char *buff, int len);
typedef int (*slt_process_func_t)(factory_t *factory, event_emit_node_t *emit_node);
typedef void (*slt_notify_func_t)(factory_t *factory, int fd, int schedid, char *data, int len);
typedef void (*slt_start_func_t)(factory_t *factory);
typedef int (*slt_worker_func_t)(factory_t *factory, event_node *data_node);
typedef pid_t (*slt_spawn_func_t)(factory_t *factory);
typedef void (*slt_done_func_t)(factory_t *factory, char *buff, int len, int fd);

typedef struct _mq_unicom {
	mqd_t mqd;
	char mqd_name[20];
} mq_unicom_t;

typedef int (*mutex_lock_func_t)(pthread_mutex_t *mutex);
typedef int (*mutex_trylock_func_t)(pthread_mutex_t *mutex);
typedef int (*mutex_unlock_func_t)(pthread_mutex_t *mutex);
typedef int (*mutex_init_func_t)(pthread_mutex_t *mutex, pthread_mutexattr_t *attr);
typedef int (*mutex_destroy_func_t)(pthread_mutex_t *mutex);

#pragma pack(1)
typedef struct _element {
	size_t allocsize;
	void *ptr;
	int fd;
	unsigned char dirty;
} element_t;
#pragma pack()

typedef struct _data_pool {
    void *mem;
	void *init_mem;
	element_t *ele_88_200;
	element_t *ele_176_190;
	element_t *ele_352_180;
	element_t *ele_704_170;
	element_t *ele_1408_160;
	element_t *ele_5632_250;
	
    pthread_mutex_t mutex;
    mutex_lock_func_t lock;
    mutex_trylock_func_t trylock;
    mutex_unlock_func_t unlock;
    mutex_init_func_t init;
    mutex_destroy_func_t destroy;
} data_pool_t;

typedef int (*slt_evfd_notify_func_t)(int evfd, int len);
typedef int (*slt_evfd_wait_func_t)(int evfd);

typedef struct _eventfd_sync {
	int evfd;

	slt_evfd_wait_func_t wait;
	slt_evfd_notify_func_t notify;
} eventfd_sync;

typedef struct _reply_sock_node reply_sock_node_t;

struct _factory {
	void *ptr;
	int fork_num;
	pid_t *child_ids;
	pid_t sub_main_id;
	mq_unicom_t unicom;
	data_pool_t *data_pool;
	eventfd_sync sync_evfd;	
	pthread_t *tids;
	zend_llist reply_sock_manage;
	

    slt_dispatch_func_t dispatch;
	slt_start_func_t start;
	slt_process_func_t process;
	slt_notify_func_t notify;
	slt_worker_func_t worker;
	slt_spawn_func_t spawn;
	slt_done_func_t done;
};


#endif
